<?php
namespace app\logic;

use app\common\corelib\CacheHelper;
use app\models\Category;
use app\models\Goods;
use app\models\Store;
use yii\db\Query;



class ShopLogic
{
    public function getCategory($mall_id){
        return Category::find()->select('distinct(c.category_id),c.category_name,c.category_sort')
            ->from('{{%mall_goods}} mg')
            ->leftJoin('{{%goods}} g','g.goods_id=mg.goods_id')
            ->leftJoin('{{%category}} c','c.category_id=g.category_id')
            ->where(['mg.mall_id'=>$mall_id,'mg.status'=>1,'g.goods_status'=>1])
            ->orderBy('c.category_sort')
            ->asArray()->all();
    }

    public function getGoodsList($mall_id,$category_id=0,$page=1,$limit=10){
        $goods = Goods::find()->select('g.goods_id,g.goods_name,g.goods_listpic,g.goods_summary,g.market_price,g.real_price')
            ->from('{{%mall_goods}} mg')
            ->leftJoin('{{%goods}} g','g.goods_id=mg.goods_id');
        if($category_id){
            $goods->where(['g.category_id'=>$category_id,'mg.mall_id'=>$mall_id,'mg.status'=>1,'g.goods_status'=>1,'g.is_del'=>0]);
        }else{
            $goods->where(['mg.mall_id'=>$mall_id,'mg.status'=>1,'g.goods_status'=>1,'g.is_del'=>0]);
        }
        $goods = $goods->orderBy('mg.sort')->offset($limit*($page-1))->limit($limit)->asArray()->all();
        foreach($goods AS &$value){
            $value['goods_listpic'] = \Yii::$app->params['pic_domain'].$value['goods_listpic'];
        }
        return $goods;
    }

    public function getGoods($goods_id){
        $goods = Goods::find()->select('goods_id,goods_name,goods_descpic,shipping_fee,market_price,real_price,goods_desc,need_idcard')
            ->where(['goods_id'=>$goods_id])
            ->asArray()->one();
        $goods_descpic = explode(',',$goods['goods_descpic']);
        foreach($goods_descpic AS &$value){
            $value = \Yii::$app->params['pic_domain'].$value;
        }
        $goods['goods_descpic'] = $goods_descpic;
        return $goods;
    }

    public function getStore($goods_id,$area_code=-1,$region_level=0,$keyword=''){
        $store = Store::find()->select('s.store_name,a1.region_name as province,a2.region_name as city,a3.region_name as district,s.address')
            ->from('{{%store}} s')
            ->leftJoin('{{%store_list}} sl','sl.store_list_id=s.store_list_id')
            ->leftJoin('{{%goods}} g','g.list_link=sl.list_link')
            ->leftJoin('{{%area}} a1','a1.region_code=s.province')
            ->leftJoin('{{%area}} a2','a2.region_code=s.city')
            ->leftJoin('{{%area}} a3','a3.region_code=s.district')
            ->where(['goods_id'=>$goods_id]);
        if($area_code!='-1' && $region_level!=0){
            if($region_level==1){
                $store->andWhere(['s.province'=>$area_code]);
            }elseif($region_level==2){
                $store->andWhere(['s.city'=>$area_code]);
            }elseif($region_level==3){
                $store->andWhere(['s.district'=>$area_code]);
            }
        }
        if($keyword){
            $store->andWhere("s.store_name LIKE '%$keyword%'");
        }
        return $store->asArray()->all();
    }

}
